import router from "@/router";
import pinia from "@/stores";
import { setUserStore } from "@/stores/user";
import "nprogress/nprogress.css";
import NProgress from "nprogress";
const userStore = setUserStore(pinia);
// console.log(userStore.token);
let whiteList = ["/login", "/404"];
router.beforeEach(async (to, from, next) => {
  NProgress.start();
  const redirectPath = to.query.redirect as string;
  if (userStore.token) {
    if (to.path == "/login") {
      // return next({ name: "HomeIndex" });
      return next(redirectPath ? redirectPath : { name: "HomeIndex" });
    }

    if (userStore.userInfo.name) {
      return next();
    } else {
      try {
        await userStore.getUserInfo();
        next(to);
      } catch (e) {
        console.log(e);
        return next({ name: "Login" });
      }
      // console.log("获取userInfo请求");
    }
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      next("/login");
    }
  }
});
router.afterEach(() => {
  // 进度条结束
  NProgress.done();
});
//****清除权限 